package com.lwz.repair.controller;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/redis")
public class redisController {
    @Autowired
    StringRedisTemplate redisTemplate;


    @PostMapping("/add")
    public String add(@RequestBody JSONObject req){
        String key =req.getString("key");
        String value=req.getString("value");


        try{
            redisTemplate.opsForValue().set(key,value);
            return "add success";


        }catch (Exception e){
            return "add failed";
        }
    }

    @PostMapping("/find")
    public String find(@RequestBody JSONObject req){
        String key =req.getString("key");


        try{
            String value= redisTemplate.opsForValue().get(key);
            return "find success"+value;


        }catch (Exception e){
            return "find failed";
        }
    }

    @PostMapping("/delete")
    public String deleta(@RequestBody JSONObject req){
        String key =req.getString("key");


        try{
           redisTemplate.delete(key);
            return "del success";


        }catch (Exception e){
            return "delfailed";
        }
    }
}
